package com.xxxx.server.controller;


import com.xxxx.server.pojo.*;
import com.xxxx.server.query.EmpQuery;
import com.xxxx.server.rest.RespBean;
import com.xxxx.server.service.impl.*;
import com.xxxx.server.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@Api("员工管理")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    //注入service
    @Resource
    private EmployeeServiceImpl employeeService;

    //注入职位
    @Resource
    private PositionServiceImpl positionService;

    //注入政治面貌
    @Resource
    private PoliticsStatusServiceImpl politicsStatusService;

    //注入民族
    @Resource
    private NationServiceImpl nationService;
    //注入职称
    @Resource
    private JoblevelServiceImpl joblevelService;

    //注入部门
    @Resource
    private DepartmentServiceImpl departmentService;

/**
 * 增删改查
 */


    /**
     * 多条件分页查询
     * @param empQuery
     * @return
     */
    @ApiOperation(value = "获取所有员工")
    @GetMapping("/basic")
    @ResponseBody
    public Map<String, Object> selectEmpByParams(EmpQuery empQuery) {//传入EmpQuery（继承基础查询类）
        return employeeService.selectEmpByParams(empQuery);
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @ApiOperation(value = "添加员工")
    @PostMapping("/basic")
    @ResponseBody
    public Integer insertEmp(@RequestBody Employee employee){
        Integer integer = employeeService.insertEmp(employee);
        return integer;
    }

    /**
     * 修改员工
     * @param employee
     * @return
     */
    @ApiOperation(value = "修改员工")
    @PutMapping("/basic")
    public Integer updateEmp(@RequestBody Employee employee){
        return employeeService.updateEmp(employee);
    }

    /**
     * 删除员工
     * @param id
     * @return
     */

    @ApiOperation(value = "删除员工")
    @DeleteMapping("/basic/{id}")
    public Integer deleteEmp(@PathVariable Integer id){
        return employeeService.deleteEmp(id);
    }

    /**
     * List下拉单
     * @return
     */
    @ApiOperation(value = "获取职位")
    @RequestMapping("/basic/positions")
    @ResponseBody
    public List<Position> selectPosition(){
        return positionService.selectPosition();
    }

    @ApiOperation(value = "获取政治面貌")
    @RequestMapping("/basic/politicsstatus")
    @ResponseBody
    public List<PoliticsStatus> selectPoliticsStatus(){
        return politicsStatusService.selectPoliticsStatus();
    }

    @ApiOperation(value = "获取民族")
    @RequestMapping("/basic/nations")
    @ResponseBody
    public List<Nation> selectNations(){
        return nationService.selectNation();
    }

    @ApiOperation(value = "获取职称")
    @RequestMapping("/basic/joblevels")
    @ResponseBody
    public List<Joblevel> selectJoblevel(){
        return joblevelService.selectJoblevel();
    }

    /**
     * 前端有bug
     * @return
     */
    @ApiOperation(value = "获取部门")
    @RequestMapping("/basic/deps")
    @ResponseBody
    public List<Department> selectDepartment(){
        return departmentService.selectDepartment();
    }

    @ApiOperation(value = "获取工号")
    @RequestMapping("/basic/maxWorkID")
    @ResponseBody
    public Map<String,Object> selectEmpMaxWorkId(){
        return employeeService.selectEmpMaxWorkId();
    }

    @ApiOperation(value = "导出员工信息数据")
    @GetMapping("/basic/export")
    public void excelExport(HttpServletResponse response) {
        //得到所有员工信息列表
        final List<Employee> list = employeeService.list();
        //导出工具类
        try {
            ExcelUtils.exportExcel(list,"员工信息表","员工信息表",
                    Employee.class,"员工信息表",response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "导入员工信息数据")
    @PostMapping("/basic/import")
    public void excelInput(@RequestParam("file") MultipartFile file) throws IOException {
        //导入工具类，得到的数据放入list集合中
        List<Employee> list = ExcelUtils.importExcel(file, Employee.class);
        //批量插入
        employeeService.saveBatch(list);
    }

    /**
     * 添加员工调动记录
      * @param et
     * @return
     */
    @ApiOperation("添加员工调动记录并修改员工表格!")
    @PostMapping("/add")
    public RespBean addETRecord(@RequestBody EmployeeTransfer et){
        return employeeService.addETRecord(et);
    }





}
